package hashtable;

import java.util.Scanner;

// 数组+链表 实现hashTable
public class HashTableDemo {
    public static void main(String[] args) {
        //创建一个hash表
        HashTab hashTab = new HashTab(7);


        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("add:添加雇员");
            System.out.println("list:显示雇员");
            System.out.println("find:查找雇员");
            System.out.println("delete:删除雇员");
            System.out.println("exit:退出系统");

            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("请输入员工id:");
                    int id = scanner.nextInt();
                    System.out.println("请输入员工姓名:");
                    String name = scanner.next();
                    Emp emp = new Emp(id, name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入员工id:");
                    id = scanner.nextInt();
                    hashTab.find(id);
                    break;
                case "break":
                    scanner.close();
                default:
                    break;
            }

        }


    }
}


//创建hash表
class HashTab {
    private EmpLinkedList[] empArray;
    private int size;

    public HashTab(int size) {
        this.size = size;
        //初始化链表
        empArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            empArray[i] = new EmpLinkedList();
        }

    }

    //添加
    public void add(Emp emp) {
        //根据员工id 得到员工应当添加到哪个链表
        int empListNo = hashFun(emp.id);
        //将emp添加到对应的链表当中
        empArray[empListNo].add(emp);
    }

    //遍历所有链表
    public void list() {
        for (int i = 0; i < size; i++) {
            empArray[i].list(i);
        }
    }
    //查找员工

    public void find(int id) {
        int emoNo = hashFun(id);
        Emp emp = empArray[emoNo].findEmpById(id);
        if (emp != null) {
            System.out.printf("在哈希表%d中找到该雇员,雇员id%d", emoNo + 1, emp.id);
        } else {
            System.out.println("在哈希表中没有找到该雇员");
        }


    }

    //编写一个散列函数 简单取模
    public int hashFun(int id) {
        return id % size;
    }

}


class Emp {
    public int id;
    public String name;
    public Emp next;  //默认为null

    public Emp(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

}


class EmpLinkedList {  //链表
    public Emp head; //默认为null

    public void add(Emp emp) {  //添加到雇员到链表
        //如果是添加第一个雇员
        if (head == null) {
            head = emp;
            return;
        }
        Emp curEmp = head;
        while (true) {
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;//后移
        }
        //加入链表
        curEmp.next = emp;
    }

    //遍历链表的雇员信息
    public void list(int no) {
        if (head == null) {
            System.out.println("第" + (no + 1) + "链表为空");
        } else {
            System.out.print("第" + (no + 1) + "链表的信息为");
            Emp curEmp = head;//辅助指针
            while (true) {
                System.out.printf("=>id=%d name=%s\t", curEmp.id, curEmp.name);
                System.out.println();
                if (curEmp.next == null) { //最后一个
                    break;
                }
                curEmp = curEmp.next;//后移 遍历
            }
        }

    }

    public Emp findEmpById(int id) {
        //判断链表是否为空
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Emp curEmp = head;
        while (true) {
            if (curEmp.id == id) {
                break;
            } else if (curEmp.next == null) {
                curEmp = null;
                break;
            }
            curEmp = curEmp.next;

        }

        return curEmp;
    }

}